/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Functions;

import java.util.ArrayList;
import operations.FunctionOperations;

/**
 *
 * @author Miro
 */
public class FixedPoint {

    private int defaultMaxIterations = 50;
    private double defaultEpsilon = 0.00001;
    private FunctionOperations myFunction;
    private int noOfIterations = 0;
    private double aprroxRoot = 0;
    private double preci = 0;
    private double beginInt = 0;
    private double endInt = 0;
    private double aGiven = 0;
    private double bGiven = 0;

    public FixedPoint() {
        // TODO Auto-generated constructor stub
    }

    public int getNoOfIts() {
        return noOfIterations;
    }

    public double approxRoot() {
        return aprroxRoot;
    }

    public double getPrecision() {
        return preci;
    }

    public String getInterval() {
        String x = "[" + beginInt + "," + endInt + "]";

        return x;

    }

    public double errorBound() {
        ErrorBound e = new ErrorBound();
        double error = e.calculateErrorBound(noOfIterations, myFunction,
                aGiven, bGiven, beginInt, endInt);
        return error;
    }

    public double[][] Falseposition(String function, double a, double b,
            double delta, int numOfItrs) {
        aGiven = a;
        ArrayList<Double> result = new ArrayList<Double>();
        double[][] returned;
        myFunction = new FunctionOperations(function);


        if (numOfItrs == -1) {
            noOfIterations = defaultMaxIterations;
        } else {
            noOfIterations = numOfItrs;
        }
        if (delta == -1) {
            preci = delta;
        } else {
            preci = defaultEpsilon;
        }
        //System.out.print(a + "       kjkj");
        //double p0 = myFunction.evaluate(a);
        result.add(a);
        double p0 = a;
        double p1 = p0;
        for (int i = 1; i < noOfIterations; i++) {
            p1 = myFunction.evaluate(i - 1);
            double err = Math.abs(p1 - p0);
            double reltiveError = err / (Math.abs(p1+preci));
            if (err < preci || reltiveError < preci) {
                break;
            }
            p0 = p1;
            result.add(p1);




        }
        noOfIterations = result.size();
        aprroxRoot = result.get(result.size() - 1);
        returned = new double[result.size()][2];
        for (int i = 0; i < result.size(); i++) {
            returned[i][0] = result.get(i);
            System.out.print(returned[i][0] + "     ");
            returned[i][1] = myFunction.evaluate(result.get(i));
            System.out.println(returned[i][1] + "     ");
        }

        return returned;
    }

    public static void main(String[] args) {
        FixedPoint f = new FixedPoint();
        f.Falseposition("exp(-x)", 0.5, -1, 0.001, -1);

    }
}
